<template>
	  <view class="container">
		  

		<view class="modal">
			<view>
				<uv-toast ref="toast"></uv-toast>
			</view>
			    <view class="modal-content">
				      <view class="modal-header">
					        <h2>{{this.lanuage.configuration}}</h2>
					        <i class="fa fa-times" @click="closeModal"></i>
					      </view>
				      <view class="modal-body" style="width:800px;">
					     <view>
						<table style="width: 100%;">
							<tr>
								<td>EC：</td>
								<td style="min-width: 50px;">{{this.lanuage.lowerlimit}}</td>
								<td><input type="number" class="txtinput" id="warnecDown" v-model="warnecDown"
										value="0" /></td>
								<td style="min-width:50px;">{{this.lanuage.upperlimit}}</td>
								<td><input type="number" class="txtinput" id="warnecUp" v-model="warnecUp" value="0" />
								</td>
								<td>
									<switch :checked="warnecisCheck" @change="switchwarnecChange()" />
								</td>
								<td><button class="butset2"
										@click="setLinkageConditionValue(114,warnecDown);setLinkageConditionValue(118,warnecDown);setLinkageConditionValue(116,warnecUp);setLinkageConditionValue(119,warnecUp);modalSetStatus(42,warnecisCheck,35);">{{this.lanuage.save}}</button>
								</td>
							</tr>
							<tr>
								<td>PH：</td>
								<td>{{this.lanuage.lowerlimit}}</td>
								<td><input type="number" class="txtinput" id="warnphDown" v-model="warnphDown"
										value="0" /></td>
								<td>{{this.lanuage.upperlimit}}</td>
								<td><input type="number" class="txtinput" id="warnphUp" v-model="warnphUp" value="0" />
								</td>
								<td>
									<switch :checked="warnphisCheck" @change="switchwarnphChange()" />
								</td>
								<td><button class="butset2"
										@click="setLinkageConditionValue(121,warnphDown);setLinkageConditionValue(125,warnphDown);setLinkageConditionValue(123,warnphUp);setLinkageConditionValue(126,warnphUp);modalSetStatus(43,warnphisCheck,36);">{{this.lanuage.save}}</button>
								</td>
							</tr>
							<tr>
								<td>{{this.lanuage.watertemp}}：</td>
								<td>{{this.lanuage.lowerlimit}}</td>
								<td><input type="number" class="txtinput" id="warnwatertempDown"
										v-model="warnwatertempDown" value="0" /></td>
								<td>{{this.lanuage.upperlimit}}</td>
								<td><input type="number" class="txtinput" id="warnwatertempUp" v-model="warnwatertempUp"
										value="0" /></td>
								<td>
									<switch :checked="warnwatertempisCheck" @change="switchwarnwatertempChange()" />
								</td>
								<td><button class="butset2"
										@click="setLinkageConditionValue(128,warnwatertempDown);setLinkageConditionValue(132,warnwatertempDown);setLinkageConditionValue(130,warnwatertempUp);setLinkageConditionValue(133,warnwatertempUp);modalSetStatus(44,warnwatertempisCheck,37);">{{this.lanuage.save}}</button>
								</td>
							</tr>

							<tr>
								<td>{{this.lanuage.LightIntensity}}：</td>
								<td>{{this.lanuage.lowerlimit}}</td>
								<td><input type="number" class="txtinput" id="warnlightDown" v-model="warnlightDown"
										value="0" /></td>
								<td>{{this.lanuage.upperlimit}}</td>
								<td><input type="number" class="txtinput" id="warnlightUp" v-model="warnlightUp"
										value="0" /></td>
								<td>
									<switch :checked="warnlightisCheck" @change="switchwarnlightChange()" />
								</td>
								<td><button class="butset2"
										@click="setLinkageConditionValue(145,warnlightDown);setLinkageConditionValue(151,warnlightDown);setLinkageConditionValue(148,warnlightUp);setLinkageConditionValue(152,warnlightUp);modalSetStatus(45,warnlightisCheck,38);">{{this.lanuage.save}}</button>
								</td>
							</tr>
							<tr>
								<td>{{this.lanuage.Temperature}}：</td>
								<td>{{this.lanuage.lowerlimit}}</td>
								<td><input type="number" class="txtinput" id="warntempDown" v-model="warntempDown"
										value="0" /></td>
								<td>{{this.lanuage.upperlimit}}</td>
								<td><input type="number" class="txtinput" id="warntempUp" v-model="warntempUp"
										value="0" /></td>
								<td>
									<switch :checked="warntempisCheck" @change="switchwarntempChange()" />
								</td>
								<td><button class="butset2"
										@click="setLinkageConditionValue(155,warntempDown);setLinkageConditionValue(161,warntempDown);setLinkageConditionValue(159,warntempUp);setLinkageConditionValue(162,warntempUp);modalSetStatus(46,warntempisCheck,39);">{{this.lanuage.save}}</button>
								</td>
							</tr>
							<tr>
								<td>{{this.lanuage.Humidity}}：</td>
								<td>{{this.lanuage.lowerlimit}}</td>
								<td><input type="number" class="txtinput" id="warnhumdown" v-model="warnhumdown"
										value="0" /></td>
								<td>{{this.lanuage.upperlimit}}</td>
								<td><input type="number" class="txtinput" id="warnhumUp" v-model="warnhumUp"
										value="0" /></td>
								<td>
									<switch :checked="warnhumisCheck" @change="switchwarnhumChange()" />
								</td>
								<td><button class="butset2"
										@click="setLinkageConditionValue(165,warnhumdown);setLinkageConditionValue(171,warnhumdown);setLinkageConditionValue(168,warnhumUp);setLinkageConditionValue(172,warnhumUp);modalSetStatus(47,warnhumisCheck,40);">{{this.lanuage.save}}</button>
								</td>
							</tr>
							<tr>
								<td>CO₂：</td>
								<td>{{this.lanuage.lowerlimit}}</td>
								<td><input type="number" class="txtinput" id="warnco2down" v-model="warnco2down"
										value="0" /></td>
								<td>{{this.lanuage.upperlimit}}</td>
								<td><input type="number" class="txtinput" id="warnco2Up" v-model="warnco2Up"
										value="0" /></td>
								<td>
									<switch :checked="warnco2isCheck" id="switchco2"  @change="switchwarnco2Change" />
								</td>
								<td><button class="butset2"
										@click="setLinkageConditionValue(176,warnco2down);setLinkageConditionValue(182,warnco2down);setLinkageConditionValue(178,warnco2Up);setLinkageConditionValue(183,warnco2Up);modalSetStatus(48,warnco2isCheck,41);">{{this.lanuage.save}}</button>
								</td>
							</tr>
						</table>


					</view>



					   </view>


				      <view class="modal-footer">
					        <button type="button" class="but1" @click="closeModal">{{this.lanuage.return}}</button>

					  <button type="button" class="but2"
						@click="modalControlSend(34,'open')">{{this.lanuage.open}}</button>
					  <button type="button" class="but2"
						@click="modalControlSend(34,'close')">{{this.lanuage.close}}</button>

					      </view>
				    </view>
			  
		</view>





		  
	</view>
</template>

<script>
	export default {
		
		data() {
			return {
				warnecDown: 0,
				warnecUp: 0,
				warnecisCheck: true,
				warnphDown: 0,
				warnphUp: 0,
				warnphisCheck: true,
				warnwatertempDown: 0,
				warnwatertempUp: 0,
				warnwatertempisCheck: true,
				warnlightDown: 0,
				warnlightUp: 0,
				warnlightisCheck: true,
				warntempDown: 0,
				warntempUp: 0,
				warntempisCheck: true,
				warnhumdown: 0,
				warnhumUp: 0,
				warnhumisCheck: true,
				warnco2down: 0,
				warnco2Up: 0,
				warnco2isCheck: true,
				warnmsg: '',

				lanuage: {
					open: '',
					close: '',
					save: '',
					Runing: '',
					IsDoing: '',
					OperationSuccessful: '',
					OperationFaild: '',
					watertemp: '',
					LightIntensity: '',
					Temperature: '',
					Humidity: '',
					upperlimit: '',
					lowerlimit: '',
					configuration: '',
					return: '',
					LinkageConditionValue: '',
					ValueCurrentByProduct: ''
				}
			}
		},
		onReady() {

			// #ifdef APP-PLUS
			console.log('APP端');
			//this.showApp = true; //该代码仅在app中生效
			// #endif

			this._freshing = false;
			this.timer = setInterval(() => {
				/* 列表 */
			//	this.getProductWarn();
			}, 5000);
			this.timer1 = setInterval(() => {
				/* 列表 */
				this.getLinkageConditionValue();
				this.getValueCurrentByProduct();
			}, 10000);
		},
		methods: {
			async getLinkageConditionValue() {

				const ret = await this.$myRequest({
					url: "/api/product/p1009/LinkageCondition/getLinkageConditionValue?pguid=" + uni
						.getStorageSync('productGuid'),
					data: {
						//pguid: uni.getStorageSync('productGuid'),
					}
				})
				if (ret.respCode == "200") {
					
					if (this.LinkageConditionValue == JSON.stringify(ret.data)) {
						return;
					}
					this.LinkageConditionValue = JSON.stringify(ret.data);
					
					ret.data.some((item, i) => {
						if (item.modelNo == 114) {
							this.warnecDown = item.value;
						} else if (item.modelNo == 116) {
							this.warnecUp = item.value;
						} else if (item.modelNo == 121) {
							this.warnphDown = item.value;
						} else if (item.modelNo == 123) {
							this.warnphUp = item.value;
						} else if (item.modelNo == 128) {
							this.warnwatertempDown = item.value;
						} else if (item.modelNo == 130) {
							this.warnwatertempUp = item.value;
						} else if (item.modelNo == 145) {
							this.warnlightDown = item.value;
						} else if (item.modelNo == 148) {
							this.warnlightUp = item.value;
						} else if (item.modelNo == 155) {
							this.warntempDown = item.value;
						} else if (item.modelNo == 159) {
							this.warntempUp = item.value;
						} else if (item.modelNo == 165) {
							this.warnhumdown = item.value;
						} else if (item.modelNo == 168) {
							this.warnhumUp = item.value;
						} else if (item.modelNo == 176) {
							this.warnco2down = item.value;
						} else if (item.modelNo == 178) {
							this.warnco2Up = item.value;
						}
					})
					//console.log('sucess' + JSON.stringify(ret.data));

				} else {
					uni.showToast({
						title: this.lanuage.OperationFaild,
						//将值设置为 success 或者直接不用写icon这个参数
						icon: 'none',
						//显示持续时间为 2秒
						duration: 1000
					})
					console.log('fail' + JSON.stringify(ret.data));
				}

			},
			async setLinkageConditionValue(modelno, value) {
				uni.showToast({
					title: this.lanuage.IsDoing,
					//将值设置为 success 或者直接不用写icon这个参数
					icon: 'success',
					//显示持续时间为 2秒
					duration: 500
				})


				const ret = await this.$myRequest({
					url: "/api/product/p1009/LinkageCondition/setLinkageConditionValue?pguid=" + uni
						.getStorageSync('productGuid') + "&val=" + value + "&modelno=" + modelno,
					data: {
						// pguid: uni.getStorageSync('productGuid'),
						// val: value,
						// modelno: modelno
					}
				})
				if (ret.respCode == "200") {
					//this.getLinkageConditionValue();
					uni.showToast({
						title: this.lanuage.OperationSuccessful,
						//将值设置为 success 或者直接不用写icon这个参数
						icon: 'success',
						//显示持续时间为 2秒
						duration: 500
					})
					//	console.log('sucess' + JSON.stringify(ret.data));

				} else {
					uni.showToast({
						title: this.lanuage.OperationFaild,
						//将值设置为 success 或者直接不用写icon这个参数
						icon: 'none',
						//显示持续时间为 2秒
						duration: 1000
					})
					console.log('fail' + JSON.stringify(ret.data));
				}

			},
			closeModal() {
				// uni.navigateTo({
				// 	url: "setParameter"
				// })
				uni.navigateBack();
			},
			modalSetStatus(modelno, warnischeck, baojingno) {
				console.log(warnischeck);
				if (warnischeck == true) {
					this.sendDeviceControl(modelno, "yes");
				} else {
					this.sendDeviceControl(modelno, "no");
					this.sendDeviceControl(baojingno, "no");
				}


				var parms = {
					message: this.lanuage.OperationSuccessful,
					duration: 1000
				};

				this.$refs.toast.show({
					...parms,
					complete() {}
				});
			},
			modalControlSend(modelno, key) {
				this.sendDeviceControl(modelno, key);
				var parms = {
					message: this.lanuage.OperationSuccessful,
					duration: 1000
				};

				this.$refs.toast.show({
					...parms,
					complete() {}
				});
			},
			switchwarnecChange() {
				if (this.warnecisCheck == true) {
					this.warnecisCheck = false;
				} else {
					this.warnecisCheck = true;
				}
			},
			switchwarnphChange() {
				if (this.warnphisCheck == true) {
					this.warnphisCheck = false;
				} else {
					this.warnphisCheck = true;
				}
			},
			switchwarnwatertempChange() {
				if (this.warnwatertempisCheck == true) {
					this.warnwatertempisCheck = false;
				} else {
					this.warnwatertempisCheck = true;
				}
			},
			switchwarnlightChange() {
				if (this.warnlightisCheck == true) {
					this.warnlightisCheck = false;
				} else {
					this.warnlightisCheck = true;
				}
			},
			switchwarntempChange() {
				if (this.warntempisCheck == true) {
					this.warntempisCheck = false;
				} else {
					this.warntempisCheck = true;
				}
			},
			switchwarnhumChange() {
				if (this.warnhumisCheck == true) {
					this.warnhumisCheck = false;
				} else {
					this.warnhumisCheck = true;
				}
			},
			switchwarnco2Change() {				
				if (this.warnco2isCheck == true) {
					this.warnco2isCheck = false;
				} else {
					this.warnco2isCheck = true;
				}
			},
			async sendDeviceControl(modelno, key) {
				uni.showToast({
					title: this.lanuage.IsDoing,
					//将值设置为 success 或者直接不用写icon这个参数
					icon: 'success',
					//显示持续时间为 2秒
					duration: 500
				})
				const ret = await this.$myRequest({
					url: "/api/product/p1009/Device/sendDeviceControl?pguid=" + uni.getStorageSync(
						'productGuid') + "&key=" + key + "&modelno=" + modelno,
					data: {
						// pguid: uni.getStorageSync('productGuid'),
						// key: key,
						// modelno: modelno
					}
				})
				if (ret.respCode == "200") {
					uni.showToast({
						title: this.lanuage.OperationSuccessful,
						//将值设置为 success 或者直接不用写icon这个参数
						icon: 'success',
						//显示持续时间为 2秒
						duration: 1000
					})
					//console.log('sucess' + JSON.stringify(ret.data));

				} else {
					uni.showToast({
						title: this.lanuage.OperationFaild,
						//将值设置为 success 或者直接不用写icon这个参数
						icon: 'none',
						//显示持续时间为 2秒
						duration: 2000
					})
					console.log('fail' + JSON.stringify(ret.data));
				}
			},
			async getPruductLanguage() {
				var languagecode = uni.getStorageSync('languagecode');
				const ret = await this.$myRequest({
					url: "/api/common/Language/getPruductLanguageAlone?pguid=" + uni.getStorageSync(
						'productGuid') + "&languagecode=" + languagecode,
					data: {
						//pguid: uni.getStorageSync('productGuid'),
					}
				})
				if (ret.respCode == "200") {
					
					
					var cCode = 0;
					var content = "";
					ret.data.some((item, i) => {
						cCode = item.code;
						content = item.content;
						if (cCode == 1000) {
							this.lanuage.open = content;
						} else if (cCode == 1001) {
							this.lanuage.close = content;
						} else if (cCode == 1005) {
							this.lanuage.save = content;
						} else if (cCode == 1025) {
							this.lanuage.light = content;
						} else if (cCode == 1026) {
							this.lanuage.co2 = content;
						} else if (cCode == 1035) {
							this.lanuage.OperationSuccessful = content;
						} else if (cCode == 1034) {
							this.lanuage.OperationFaild = content;
						} else if (cCode == 1036) {
							this.lanuage.IsDoing = content;
						} else if (cCode == 1031) {
							this.lanuage.Humidity = content;
						} else if (cCode == 1043) {
							this.lanuage.watertemp = content;
						} else if (cCode == 1038) {
							this.lanuage.LightIntensity = content;
						} else if (cCode == 1030) {
							this.lanuage.Temperature = content;
						} else if (cCode == 1050) {
							this.lanuage.upperlimit = content;
						} else if (cCode == 1051) {
							this.lanuage.lowerlimit = content;
						} else if (cCode == 1053) {
							this.lanuage.configuration = content;
						} else if (cCode == 1054) {
							this.lanuage.return = content;
						}
						

					});

					//	console.log('sucess' + JSON.stringify(ret.data));

				} else {
					uni.showToast({
						title: this.lanuage.OperationFaild,
						//将值设置为 success 或者直接不用写icon这个参数
						icon: 'none',
						//显示持续时间为 2秒
						duration: 2000
					})
					console.log('fail' + JSON.stringify(ret.data));
				}

			},
			async getValueCurrentByProduct() {

				const ret = await this.$myRequest({
					url: "/api/product/p1009/DeviceValueCurrent/getValueCurrentByProduct?pguid=" + uni
						.getStorageSync('productGuid'),
					data: {
						//pguid: uni.getStorageSync('productGuid')
					}
				})
				//uni.stopPullDownRefresh(); // 停止当前页面刷新
				if (ret.respCode == "200") {

					if (this.ValueCurrentByProduct == JSON.stringify(ret.data)) {
						return;
					}
					this.ValueCurrentByProduct = JSON.stringify(ret.data);
					
					
					
					//	console.log('sucess' + JSON.stringify(ret.data));
					ret.data.some((item, i) => {

						if (item.modelNo == "42" && item.key == "status") {
							if (item.value == "yes") {
								this.warnecisCheck = true;
							} else {
								this.warnecisCheck = false;
							}
						} else if (item.modelNo == "43" && item.key == "status") {
							if (item.value == "yes") {
								this.warnphisCheck = true;
							} else {
								this.warnphisCheck = false;
							}
						} else if (item.modelNo == "44" && item.key == "status") {
							if (item.value == "yes") {
								this.warnwatertempisCheck = true;
							} else {
								this.warnwatertempisCheck = false;
							}
						} else if (item.modelNo == "45" && item.key == "status") {
							if (item.value == "yes") {
								this.warnlightisCheck = true;
							} else {
								this.warnlightisCheck = false;
							}
						} else if (item.modelNo == "46" && item.key == "status") {
							if (item.value == "yes") {
								this.warntempisCheck = true;
							} else {
								this.warntempisCheck = false;
							}
						} else if (item.modelNo == "47" && item.key == "status") {
							if (item.value == "yes") {
								this.warnhumisCheck = true;
							} else {
								this.warnhumisCheck = false;
							}
						} else if (item.modelNo == "48" && item.key == "status") {
							if (item.value == "yes") {
								this.warnco2isCheck = true;
							} else {
								this.warnco2isCheck = false;
							}
						}
					})
					// this.banner = data.data;
				} else {
					console.log('fail' + JSON.stringify(ret.data));
				}


			}


		},
		onUnload() {
		
			//if (this.timer) {
			clearInterval(this.timer)
			this.timer = null
		
			clearInterval(this.timer1)
			this.timer1 = null
			//}
		},
		onHide() {
			//if (this.timer) {
			clearInterval(this.timer)
			this.timer = null
		
			clearInterval(this.timer1)
			this.timer1 = null
			//}
		},
		onLoad(options) {
			this.getPruductLanguage();
			this.getLinkageConditionValue();
			this.getValueCurrentByProduct();
		}
	}
</script>
<style lang="scss">
	.container {
		position: absolute;
		width: 100%;
		height: 100%;
		//background-color:darkblue;
		//background-color: #f3f1f1;
		//background-color: #28c32b;
		background-image: url("../../static/imageV7/bg3.jpeg");
		background-size: 100% 100%;
		font-family: '微软雅黑';

	}

	.modal .txtinput {
		width: 80px;
		height: 35px;
		background-color: #b0e2a3;
		text-align: center;
	}

	.modal {
		position: fixed;
		left: 0;
		top: 0;
		z-index: 99999;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.modal .modal-content {

		background-color: #ffffff;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.modal .modal-content .modal-header {
		background-color: green;
		color: #ffffff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10rpx;
	}

	.modal .modal-content .modal-header h2 {
		margin: 0;
	}

	.modal .modal-content .modal-header i {
		font-size: 20rpx;
		cursor: pointer;
	}

	.modal .modal-content .modal-body {
		padding: 20rpx;
	}

	.modal .modal-content .modal-footer {
		background-color: #eeeeee;
		padding: 10rpx;

		display: flex;
		justify-content: flex-end;
	}

	.modal .modal-content .modal-footer button {
		border: none;
		margin-left: 10rpx;
		margin: 10px;
		width: 100px;
		border-radius: 5rpx;
		cursor: pointer;
	}

	.modal .modal-content .modal-footer .but1 {
		background-color: green;
		color: #ffffff;
	}

	.modal .modal-content .modal-footer .but2 {
		background-color: #dddddd;
		color: #333333;
	}
</style>